import 'package:intl/intl.dart';
import 'package:flutter/material.dart';

class DateUtil {
  static date2FmtStr(DateTime d, [String f = 'yyyy-MM-dd']) {
    DateFormat format = DateFormat(f);
    String result = format.format(d);
    return result;
  }
}

class SwitchAnimate {
  static createTransition(Animation<double> animation, Widget widget) {
    return SlideTransition(
      position:
          Tween<Offset>(begin: const Offset(-1, 0), end: const Offset(0, 0))
              .animate(animation),
      child: widget,
    );
  }
}

class DialogUtil {
  static createDialog(BuildContext context, String title, String content,
      [Function callback]) {
    showDialog(
        context: context,
        builder: (_) => AlertDialog(
              title: Text('提示'),
              content: Text(content),
              actions: <Widget>[
                FlatButton(
                    onPressed: () => Navigator.pop(context), child: Text('确定'))
              ],
            ));
  }

  static createLoading(BuildContext context, String loadingText, Future request,
      Function response) {
    print('已经进入了dialog方法');
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (_) {
          return LoadingDialog(
            loadingText: loadingText,
            requestCallBack: request,
            responseCallBack: response,
          );
        });
  }
}

class LoadingDialog extends StatefulWidget {
  final String loadingText;

  final bool outsideDismiss;

  final Function responseCallBack;

  final Future requestCallBack;

  LoadingDialog({
    Key key,
    this.loadingText,
    this.outsideDismiss = true,
    this.responseCallBack,
    this.requestCallBack,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _LoadingState();
  }
}

class _LoadingState extends State<LoadingDialog> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    //当回调函数不为空的时候，先进行渲染，然后执行回调函数，执行完成后直接退出当前页面
    if (widget.requestCallBack != null) {
      widget.requestCallBack.then((dynamic res) {
        Navigator.pop(context);
        widget.responseCallBack(res);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Material(
        type: MaterialType.transparency,
        child: Center(
          child: SizedBox(
            width: 120,
            height: 120,
            child: Container(
              decoration: ShapeDecoration(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(8.0))),
                color: Colors.white,
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  CircularProgressIndicator(),
                  Padding(
                    padding: EdgeInsets.only(top: 20),
                    child: Text(
                      widget.loadingText,
                      style: TextStyle(fontSize: 14),
                    ),
                  )
                ],
              ),
            ),
          ),
        ));
  }
}
